package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.AgentiaCategoryMapper;
import com.zhijian.medical.entity.po.AgentiaCategory;
import com.zhijian.medical.entity.vo.request.AgentiaCategorySearchVo;
import com.zhijian.medical.entity.vo.response.AgentiaCategoryVo;
import com.zhijian.medical.enums.ResponseCodeEnum;
import com.zhijian.medical.exception.BusinessException;
import com.zhijian.medical.service.AgentiaCategoryService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 试剂品类业务处理层
 *
 * @author HCG
 * @date 2022-01-13 15:43:44
 * @version: 1.0
 */
@Slf4j
@Service
public class AgentiaCategoryServiceImpl implements AgentiaCategoryService {

    @Autowired
    private AgentiaCategoryMapper mapper;

    @Override
    public AgentiaCategory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AgentiaCategoryVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(AgentiaCategory agentiaCategory) {
        Integer count = this.count(AgentiaCategorySearchVo.builder()
                .code(agentiaCategory.getCode())
                .build());
        if (count > 0) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "此编号重复，请重新输入");
        }
        Date now = new Date();
        agentiaCategory.setCreateTime(now);
        agentiaCategory.setUpdateTime(now);
//        agentiaCategory.setCode(createNextCode());
        return mapper.insert(agentiaCategory) == 1 ? agentiaCategory.getId() : 0L;
    }

    @Override
    public boolean update(AgentiaCategory agentiaCategory) {
        Integer count = this.count(AgentiaCategorySearchVo.builder()
                .notId(agentiaCategory.getId())
                .code(agentiaCategory.getCode())
                .build());
        if (count > 0) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "此编号重复，请重新输入");
        }
        Date now = new Date();
        agentiaCategory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(agentiaCategory) != 0;
    }

    @Override
    public Integer count(AgentiaCategorySearchVo agentiaCategorySearchVo) {
        return mapper.count(agentiaCategorySearchVo);
    }

    @Override
    public List<AgentiaCategoryVo> selectList(AgentiaCategorySearchVo agentiaCategorySearchVo) {
        return mapper.selectList(agentiaCategorySearchVo);
    }

    @Override
    public Page<AgentiaCategoryVo> selectPage(AgentiaCategorySearchVo agentiaCategorySearchVo) {
        //先直接查总数
        int count = count(agentiaCategorySearchVo);
        //分页
        Page<AgentiaCategoryVo> page = new Page<>(true, count, agentiaCategorySearchVo.getPageNum(), agentiaCategorySearchVo.getPageSize());
        agentiaCategorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AgentiaCategoryVo> responseVos = this.selectList(agentiaCategorySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public String selectMaxCode() {
        return mapper.selectMaxCode();
    }

    @Override
    public String createNextCode() {
        String maxCodeStr = selectMaxCode();
        if (StringUtils.isBlank(maxCodeStr)) {
            return "FZ0001";
        }
        Integer maxCode = Integer.valueOf(maxCodeStr.replace("FZ", ""));
        maxCode++;
        return "FZ" + String.format("%04d", maxCode);
    }

}